package org.xs.object.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.xs.object.dao.product.Product;
import org.xs.object.dao.page.PageResquest;
import org.xs.object.service.impl.ProductService;
import org.xs.object.utils.R;
import org.xs.object.utils.ResponseEnum;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/product")
public class ProductController {
    private ProductService productService;
    @Autowired
    public ProductController(ProductService productService){
        this.productService=productService;
    }

    //查询商品列表(分页)
    @PostMapping("/productList")
    public R productList(@RequestBody PageResquest pageRequest){
        return productService.postProductList(pageRequest);
    }

    //根据商品ID查询商品详情
    @GetMapping("/getProductById/{id}")
    public R getProductById(@PathVariable Integer id){
        return R.ok(productService.getProductById(id));
    }
    @PostMapping("/getProductsById")
    public R getProductsById(@RequestBody(required = false) ArrayList<Integer> ids){
        return R.ok(productService.getProductById(ids));
    }
    //根据名称查询商品
    @PostMapping("/getProductsByName")
    public R getProductsByName(@RequestBody String name){
        return R.ok(productService.getProductsByName(name));
    }
    @PostMapping("/getProductsByNames")
    public R getProductsByName(@RequestBody ArrayList<String> names){
        return R.ok(productService.getProductsByName(names));
    }
    //根据品牌查询商品
    @GetMapping("/getProductsByBrand/{brand}")
    public R getProductsByBrand(@PathVariable String brand){
        return R.ok(productService.getProductsByBrand(brand));
    }
    @PostMapping("/getProductsByBrand")
    public R getProductsByBrand(@RequestBody ArrayList<String> brands){
        return R.ok(productService.getProductsByBrand(brands));
    }
    //修改商品
    @PutMapping("/updateProduct")
    public R updateProduct(@RequestBody Product product){
        return R.ok(productService.updateProduct(product));
    }
    @PutMapping("/updateProducts")
    public R updateProducts(@RequestBody ArrayList<Product>  products){
        return R.ok(productService.updateProduct(products));
    }
    //上传商品logo
    @PostMapping("/uploadProductLogo")
    public R uploadProductLogo(@RequestBody MultipartFile file){
        return R.ok(productService.uploadProductLogo(file));
    }
    //删除商品logo
    @DeleteMapping("/deleteProductLogo/{id}")
    public R deleteProductLogo(@PathVariable Integer id){
        return R.ok(productService.deleteProductLogo(id));
    }
    //删除商品
    @DeleteMapping("/deleteProduct/{id}")
    public R deleteProduct(@PathVariable Integer id){
        return R.ok(productService.deleteProduct(id));
    }
    @DeleteMapping("/deleteProducts")
    public R deleteProducts(@RequestBody ArrayList<Integer> ids){
        return R.ok(productService.deleteProduct(ids));
    }
    //添加商品
    @PostMapping("/addProduct")
    public R addProduct(@RequestBody Product product){
        return R.ok(productService.addProduct(product));
    }
    @PostMapping("/addProducts")
    public R addProducts(@RequestBody ArrayList<Product>  products){
        return R.ok(productService.addProduct(products));
    }

}
